package com.sam;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Driver;

public class Post {
	int id;
	String userId;
	String nickName;
	String titleName;
	String conts;
	ArrayList<Post> allPosts;
	
	public Post(){
		
	}
	
	protected Post(int id, String userId, String nickName, String titleName,
			String conts) {
		super();
		this.id = id;
		this.userId = userId;
		this.nickName = nickName;
		this.titleName = titleName;
		this.conts = conts;
	}
	
	protected Post(String userId, String nickName, String titleName,
			String conts) {
		super();
		this.userId = userId;
		this.nickName = nickName;
		this.titleName = titleName;
		this.conts = conts;
	}

	public int delete(int id){
		int rowCount = 0;
		try {
			DriverManager.registerDriver(new Driver());
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8",
					"j102u17", "j102u17.989");
			String sql = "delete from guestbook where id=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rowCount = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rowCount;
	}
	
	public ArrayList<Post> getAllPosts(){
		if (allPosts==null)
			allPosts = new ArrayList<Post>();
		allPosts.clear();
		try {
			DriverManager.registerDriver(new Driver());
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8",
					"j102u17", "j102u17.989");
			String sql = "select id, userId, nickName, titleName, Conts from guestbook order by id, nickName";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				int id = rs.getInt("id");
				String userId = rs.getString("userId");
				String nickName = rs.getString("nickName");
				String titleName = rs.getString("titleName");
				String Conts = rs.getString("Conts");
				Post p = new Post(id, userId, nickName, titleName, Conts );
				allPosts.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return allPosts;
	}
	
	public boolean save(){
		boolean saved = false;
		try {
			DriverManager.registerDriver(new Driver());
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8",
					"j102u17", "j102u17.989");
			String sql = "insert into guestbook(userid, nickname, titleName, conts) values (?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			pstmt.setString(2, nickName);
			pstmt.setString(3, titleName);
			pstmt.setString(4, conts);
			int rowCount = pstmt.executeUpdate();
			if (rowCount==1){
				saved = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return saved;
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getTitleName() {
		return titleName;
	}

	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}

	public String getConts() {
		return conts;
	}

	public void setConts(String conts) {
		this.conts = conts;
	}

	public void setAllPosts(ArrayList<Post> allPosts) {
		this.allPosts = allPosts;
	}
	
}


